﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Advantech.Adam;
using System.Net.Sockets;

namespace MixingStation
{
    public partial class InfraRedUC : UserControl
    {
        private InfraRedParameter m_Parameter = null;
        private AdamSocket m_AdamSocket;

        public InfraRedUC()
        {
            InitializeComponent();
        }

        public void InitInfraRedUC(InfraRedParameter infraRedParameter)
        {
            m_Parameter = infraRedParameter;
            if (infraRedParameter.InfraRedWay == 0)
                return;
            else if (infraRedParameter.InfraRedWay == 1)
            {
                //PCI板卡控制红外
                try
                {
                    PCIControl.SmedDllInit();
                }
                catch (Exception ex)
                {
                    pBRedF.BackColor = Color.Red;
                    pBRedB.BackColor = Color.Red;
                    string errInfo = "PCI板卡红外初始化异常:" + ex.Message;
                    MessageBox.Show(errInfo);
                    LogHelper.error(errInfo);
                    return;
                }
                timer_RedStatus.Enabled = true;
            }
            else if (infraRedParameter.InfraRedWay == 2)
            {
                //IO控制红外
                m_AdamSocket = new AdamSocket();
                m_AdamSocket.SetTimeout(1000, 1000, 1000);
                try
                {
                    if (!m_AdamSocket.Connect(infraRedParameter.IPAddrOfInfraIO, ProtocolType.Tcp, 502))
                    {
                        pBRedF.BackColor = Color.Red;
                        pBRedB.BackColor = Color.Red;
                        string errInfo = "IO红外初始化连接失败！";
                        MessageBox.Show(errInfo);
                        LogHelper.error(errInfo);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    pBRedF.BackColor = Color.Red;
                    pBRedB.BackColor = Color.Red;
                    string errInfo = "IO红外初始化异常:" + ex.Message;
                    MessageBox.Show(errInfo);
                    LogHelper.error(errInfo);
                    return;
                }
                timer_RedStatus.Enabled = true;
            }
        }

        //获取红外是否被遮挡
        public bool IsInfraRedShading()
        {
            if (this.pBRedF.BackColor == Color.Black &&
                 this.pBRedB.BackColor == Color.Black)
                return false;
            else
                return true;
        }

        private void timer_RedStatus_Tick(object sender, EventArgs e)
        {
            try
            {
                if (m_Parameter.InfraRedWay == 1)
                {
                    string isTurnOverStr = m_Parameter.IsTurnover.ToString();
                    string status = GetRedStatus();
                    pBRedF.BackColor = status.Substring(m_Parameter.FInfrared - 1, 1)
                        == isTurnOverStr ? Color.Red : Color.Black;//前限  1黑 0红
                    pBRedB.BackColor = status.Substring(m_Parameter.BInfrared - 1, 1)
                        == isTurnOverStr ? Color.Red : Color.Black;//前限
                }
                else
                {
                    bool[] bDiData;
                    bool isTurnOver = m_Parameter.IsTurnover == 1 ? true : false;
                    m_AdamSocket.Modbus().ReadCoilStatus(1, 6, out bDiData);
                    pBRedF.BackColor = bDiData[m_Parameter.FInfrared] == isTurnOver ? Color.Red : Color.Black;
                    pBRedB.BackColor = bDiData[m_Parameter.BInfrared] == isTurnOver ? Color.Red : Color.Black;
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取红外状态异常：" + ex.Message);
            }
        }

        private static string GetRedStatus()
        {
            string status = "";
            long red = PCIControl.CheckDi();//获取红外状态
            //将返回值取余8次,每次取余后将原值/2,8次返回的余数为红外的状态  余数为0或者1
            //1代表未被折腾,0代表被遮挡
            for (int i = 0; i < 8; i++)
            {
                long yu = red % 2;
                red = red / 2;
                status += yu.ToString();
            }
            return status;
        }
    }
}
